import Cookies from 'js-cookie'

/* key的公共污染问题，需要统一管理 */
const colorThemeKey = 'LC_ColorTheme'
const darkColorThemeKey = 'LC_DarkColorTheme'
const userInfoKey = 'LC_UserInfo'

const expires = 30

/* colorTheme 相关（亮色暗色分别保存） */
export function getColorTheme(type: 'light' | 'dark' = 'light') {
  return Cookies.get(type === 'light' ? colorThemeKey : darkColorThemeKey)
}

export function setColorTheme(val: string, type: 'light' | 'dark' = 'light') {
  Cookies.set(type === 'light' ? colorThemeKey : darkColorThemeKey, val, { expires })
}

/* 用户信息 */
export function getUserInfo() {
  try {
    const text = Cookies.get(userInfoKey)
    const obj = text && JSON.parse(text)
    return obj
  } catch (err) {
    console.log(err)
  }
}
export function setUserInfo(obj: Object) {
  try {
    if (!obj) return
    const text = JSON.stringify(obj)
    Cookies.set(userInfoKey, text, { expires })
  } catch (err) {
    console.log(err)
  }
}

export default {
  getColorTheme,
  setColorTheme,
  getUserInfo,
  setUserInfo
}
